package org.testis.utils.middleware;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import org.orm.PersistentException;
import org.orm.PersistentTransaction;
import org.testis.orm.classes.T_bed_deviceabstraction;
import org.testis.orm.classes.T_bed_deviceabstractionFactory;
import org.testis.orm.classes.T_dmap_languages;
import org.testis.orm.classes.T_dmap_languagesCriteria;
import org.testis.orm.classes.T_dmap_languagesFactory;

/**
 * 
 * @author nurhat
 */
public class LanguageDataHandler extends DbConnectionSupplier {

    private HashMap languageListToMap(T_dmap_languages[] langs) {
	HashMap hp = new HashMap();
	if (langs != null) {
	    for (int i = 0; i < langs.length; i++) {
		T_dmap_languages t_dmap_languages = langs[i];
		hp.put(t_dmap_languages.getId(), t_dmap_languages.getName());
	    }
	}
	return HashMapUtils.sortHashMapByValues(hp);
    }

    private HashMap deviceAbstractionListToMap(
	    T_bed_deviceabstraction[] abstractions) {
	HashMap hp = new HashMap();
	if (abstractions != null) {
	    for (int i = 0; i < abstractions.length; i++) {
		T_bed_deviceabstraction tmp = abstractions[i];
		hp.put(tmp.getId(), tmp.getName());
	    }
	}
	return HashMapUtils.sortHashMapByValues(hp);
    }

    public HashMap getAllDeviceAbstractionsAsMap() {
	T_bed_deviceabstraction[] abstractions = getAllDeviceAbstractions();
	return deviceAbstractionListToMap(abstractions);
    }

    public T_bed_deviceabstraction[] getAllDeviceAbstractions() {
	T_bed_deviceabstraction[] abstractions = null;
	try {
	    abstractions = T_bed_deviceabstractionFactory
		    .listT_bed_deviceabstractionByQuery(null, null);
	} catch (PersistentException ex) {
	}
	return abstractions;
    }

    public HashMap getAllLanguagesAsMap() {
	T_dmap_languages[] langs = getAllLanguages();
	return languageListToMap(langs);
    }

    public T_dmap_languages[] getAllLanguages() {
	T_dmap_languages[] langs = null;
	try {
	    langs = T_dmap_languagesFactory.listT_dmap_languagesByQuery(null,
		    null);
	} catch (PersistentException ex) {
	}
	return langs;
    }

    public HashMap getLanguagesByDeviceAbstractionIdAsMap(
	    int deviceAbstractionId) {
	T_dmap_languages[] langs = getLanguagesByDeviceAbstractionId(deviceAbstractionId);
	return languageListToMap(langs);
    }

    public T_dmap_languages[] getLanguagesByDeviceAbstractionId(
	    int deviceAbstractionId) {
	T_dmap_languages[] langs = null;
	try {
	    T_dmap_languagesCriteria langCriteria = new T_dmap_languagesCriteria();
	    langCriteria.deviceAbstractionId.eq(deviceAbstractionId);
	    langs = langCriteria.listT_dmap_languages();
	} catch (PersistentException ex) {
	}
	return langs;
    }

    public HashMap getLanguagesByDeviceIdAsMap(int deviceId) {
	T_dmap_languages[] langs = getLanguagesByDeviceId(deviceId);
	return languageListToMap(langs);
    }

    public T_dmap_languages[] getLanguagesByDeviceId(int deviceId) {
	T_dmap_languages[] langs = null;
	try {
	    T_dmap_languagesCriteria langCriteria = new T_dmap_languagesCriteria();
	    langCriteria.deviceId.eq(deviceId);
	    langs = langCriteria.listT_dmap_languages();
	} catch (PersistentException ex) {
	}
	return langs;
    }

    public HashMap getLanguagesByFirmwareIdAsMap(int firmwareId) {
	T_dmap_languages[] langs = getLanguagesByFirmwareId(firmwareId);
	return languageListToMap(langs);
    }

    public T_dmap_languages[] getLanguagesByFirmwareId(int firmwareId) {
	T_dmap_languages[] langs = null;
	try {
	    T_dmap_languagesCriteria langCriteria = new T_dmap_languagesCriteria();
	    langCriteria.firmwareId.eq(firmwareId);
	    langs = langCriteria.listT_dmap_languages();
	} catch (PersistentException ex) {
	}
	return langs;
    }

    public HashMap getLanguagePairsAsMap(int langId) {
	T_dmap_languages lang = getLanguageById(langId);
	if (lang == null) {
	    return null;
	} else {
	    return parseFile(lang.getFile());
	}
    }

    public HashMap getLanguagePairsMap(String langName,
	    int deviceAbstractionId, int deviceId, int firmwareId) {

	T_dmap_languages lang = getLanguageByFirmwareId(langName, firmwareId);
	if (lang == null) {
	    lang = getLanguageByDeviceId(langName, deviceId);
	    if (lang == null) {
		lang = getLanguageByDeviceAbstractionId(langName,
			deviceAbstractionId);
	    }
	}
	if (lang == null) {
	    return null;
	} else {
	    return parseFile(lang.getFile());
	}
    }

    public boolean deleteLanguageById(int langId) {

	T_dmap_languages lang = null;
	boolean result = false;
	if (langId == 0) {
	    return result;
	}
	try {
	    PersistentTransaction t = openDBConnection().beginTransaction();
	    try {
		T_dmap_languagesCriteria langCriteria = new T_dmap_languagesCriteria();
		langCriteria.id.eq(langId);
		lang = langCriteria.uniqueT_dmap_languages();
		result = lang.delete();
		t.commit();
	    } catch (PersistentException ex) {
		t.rollback();
	    } finally {
		closeDBConnection();
	    }
	} catch (PersistentException ex) {
	}
	return result;
    }

    public T_dmap_languages getLanguageById(int langId) {
	T_dmap_languages lang = null;
	if (langId == 0) {
	    return null;
	}
	try {
	    T_dmap_languagesCriteria langCriteria = new T_dmap_languagesCriteria();
	    langCriteria.id.eq(langId);
	    lang = langCriteria.uniqueT_dmap_languages();

	} catch (PersistentException ex) {
	}
	return lang;
    }

    public T_dmap_languages getLanguageByDeviceAbstractionId(String langName,
	    int deviceAbstractionId) {
	T_dmap_languages lang = null;
	if (deviceAbstractionId == 0) {
	    return null;
	}
	try {
	    T_dmap_languagesCriteria langCriteria = new T_dmap_languagesCriteria();
	    langCriteria.name.eq(langName);
	    langCriteria.deviceAbstractionId.eq(deviceAbstractionId);
	    lang = langCriteria.uniqueT_dmap_languages();

	} catch (PersistentException ex) {
	}
	return lang;
    }

    public T_dmap_languages getLanguageByDeviceId(String langName, int deviceId) {
	T_dmap_languages lang = null;

	if (deviceId == 0) {
	    return null;
	}
	try {
	    T_dmap_languagesCriteria langCriteria = new T_dmap_languagesCriteria();
	    langCriteria.name.eq(langName);
	    langCriteria.deviceId.eq(deviceId);
	    lang = langCriteria.uniqueT_dmap_languages();

	} catch (PersistentException ex) {
	}
	return lang;
    }

    public T_dmap_languages getLanguageByFirmwareId(String langName,
	    int firmwareId) {
	T_dmap_languages lang = null;
	if (firmwareId == 0) {
	    return null;
	}
	try {
	    T_dmap_languagesCriteria langCriteria = new T_dmap_languagesCriteria();
	    langCriteria.name.eq(langName);
	    langCriteria.firmwareId.eq(firmwareId);
	    lang = langCriteria.uniqueT_dmap_languages();

	} catch (PersistentException ex) {
	}
	return lang;
    }

    private HashMap parseFile(String fileText) {
	HashMap hmap = new HashMap();
	String str;
	BufferedReader reader = new BufferedReader(new StringReader(fileText));
	try {
	    while ((str = reader.readLine()) != null) {

		if (str.trim().length() > 0) {
		    int indexOfEq = str.indexOf("=");
		    if (indexOfEq > 0) {
			String key = str.substring(0, indexOfEq);
			key = key.trim();
			String val = str.substring(indexOfEq + 1);
			val = val.trim();
			val = val.replace("\"", "");

			if (val.endsWith(";")) {
			    val = val.substring(0, val.length() - 1);
			}
			if (key.startsWith("var")) {
			    key = key.replace("var", "");
			    key = key.trim();
			}
			hmap.put(key, val);
			// System.out.println(key + ":" + val);
			// System.out.println("-----------------------");
		    }
		}
	    }

	} catch (IOException e) {
	}
	return HashMapUtils.sortHashMapByKeys(hmap);
    }

    public boolean saveLanguage(int languageId, String langName,
	    int deviceAbstractionId, int deviceId, int firmwareId,
	    String langText) {
	boolean result = false;
	try {
	    PersistentTransaction t = openDBConnection().beginTransaction();
	    try {
		T_dmap_languagesCriteria langCriteria = new T_dmap_languagesCriteria();
		if (languageId != 0) {
		    langCriteria.id.eq(languageId);
		} else {
		    langCriteria.name.eq(langName);
		    if (deviceAbstractionId != 0) {
			langCriteria.deviceAbstractionId
				.eq(deviceAbstractionId);
		    } else if (deviceId != 0) {
			langCriteria.deviceId.eq(deviceId);
		    } else if (firmwareId != 0) {
			langCriteria.firmwareId.eq(firmwareId);
		    }
		}
		T_dmap_languages lang = langCriteria.uniqueT_dmap_languages();
		if (lang == null) {
		    lang = new T_dmap_languages();
		}
		lang.setName(langName);
		if (deviceAbstractionId != 0) {
		    lang.setDeviceAbstractionId(deviceAbstractionId);
		} else if (deviceId != 0) {
		    lang.setDeviceId(deviceId);
		} else if (firmwareId != 0) {
		    lang.setFirmwareId(firmwareId);
		}
		lang.setFile(langText);
		lang.save();
		t.commit();
		result = true;
	    } catch (PersistentException ex) {
		t.rollback();
	    } finally {
		closeDBConnection();
	    }
	} catch (PersistentException ex) {
	}
	return result;
    }
}
